<%@ page language="java" session="true" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:include page="../common/framework/header.jsp" />
<link href="${pageContext.request.contextPath}/static/plugin/bootstrap-table/bootstrap-table.min.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/static/plugin/daterangepicker/daterangepicker.min.css" rel="stylesheet">
<style>
	body{
		font-size:12px;color:#333;font-family:微软雅黑;background-color: #fff;
	}
  .form-inline .form-control {
    width: 100%;
	}
	.checkbox input[type=checkbox], .checkbox-inline input[type=checkbox], .radio input[type=radio], .radio-inline input[type=radio] {
    margin-top: 2px;
}
.checkbox, .radio {
    margin-top: 5px;
}
</style>

	<div class="container-fluid" style="padding:0 40px;">
    <form id ="userForm">

    <div class = "row" style = "margin-top : 20px;">
         	<div class="form-group col-sm-6 col-xs-6" >
            	<label class="col-xs-2" style="padding:6px 0 0 0;text-align:right;">名称:</label>
            	<div class="col-xs-10" style="padding-right:0;">
					<input type = "text" class="form-control"  id="name" name = "name" >
				</div>
         	</div>
         	<div class = "form-group col-sm-6 col-xs-6" >
            	<label class="col-xs-2" style="padding:6px 0 0 0;text-align:right;">状态:</label>
            	<div class="col-xs-10" style="padding-right:0;">
					<select class="form-control" data-width="100%" id="state" name="state" onchange="stateChange();">
                     </select>
				</div>
			</div>
		</div>
    	<div class = "row">
         	<div class="form-group col-sm-6 col-xs-6" >
            	<label class="col-xs-2" style="padding:6px 0 0 0;text-align:right;">迭代:</label>
            	<div class="col-xs-10" style="padding-right:0;">
					<select class="form-control" data-width="100%" id="iterationId" name="iterationId" onchange="iterationIdChange();">
                     </select>
				</div>
         	</div>
         	<div class = "form-group col-sm-6 col-xs-6" >
            	<label class="col-xs-2" style="padding:6px 0 0 0;text-align:right;">区域:</label>
            	<div class="col-xs-10" style="padding-right:0;">
					<select class="form-control" data-width="100%" id="areaId" name="areaId" onchange="areaIdChange();">
                     </select>
				</div>
			</div>
		</div>

		<div class = "row">
         	<div class="form-group col-sm-6 col-xs-6" >
            	<label class="col-xs-2" style="padding:6px 0 0 0;text-align:right;">创建人:</label>
            	<div class="col-xs-10" style="padding-right:0;">
					<select class="form-control" data-width="100%" id="authorId" name="authorId" onchange="authorIdChange();">
                     </select>
				</div>
         	</div>
         	<div class = "form-group col-sm-6 col-xs-6" >
            	<label class="col-xs-2" style="padding:6px 0 0 0;text-align:right;">指派人:</label>
            	<div class="col-xs-10" style="padding-right:0;">
					<select class="form-control" data-width="100%" id="assignId" name="assignId" onchange="assignIdChange();">
                     </select>
				</div>
			</div>
		</div>

		<div class = "row" >
	    	<div class = "form-group col-sm-10 col-xs-10" style="padding-left:19px;">
            	<label class="col-xs-1" style="padding:6px 0 0 0;text-align:right;">创建日期:</label>
				<div class="col-xs-11" style="padding-right:0px;">
					<div class="input-group">
						<input id="daterangepicker" name="date-range-picker"
							   class="form-control text-center">
						<div class="input-group-addon">
							<i class="fa fa-calendar"></i>
						</div>
					</div>
				</div>
			</div>
			<div class="col-sm-1 col-xs-1" style="padding-left: 0;">
				<a class="btn btn-primary" onclick="query();">
					查询
				</a>
			</div>
			<div class="col-sm-1 col-xs-1" style="padding-left: 0;">
				<a class="btn btn-gray" onclick="reset();">
					重置
				</a>
			</div>
		</div>

    </form>
    <table id="testCaseTable" class="table" >
        <thead>
		       <tr>
		           <th data-field="" data-checkbox="true"></th>
		           <th data-field="name">用例名称</th>
		           <th data-field="state" data-formatter="stateFormatter">状态</th>
		           <th data-field="areaName" >区域</th>
		           <th data-field="priority" >优先级</th>
		           <th data-field="iterationName" >迭代</th>
		           <th data-field="authorNm" >创建人</th>
		           <th data-field="assignNm" >指派人</th>
		           <th data-field="creationTs" data-formatter="timeFormatter" data-width="130">创建时间</th>
		       </tr>
		   </thead>
    </table>
</div>
	<jsp:include page="../common/framework/script.jsp" />
	<script src="${pageContext.request.contextPath}/static/js/common/bootdatatable.init.js"></script>
	<script src="${pageContext.request.contextPath}/static/plugin/bootstrap-table/bootstrap-table.min.js"></script>
	<script src="${pageContext.request.contextPath}/static/plugin/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
	<script src="${pageContext.request.contextPath}/static/plugin/moment-develop/moment.min.js"></script>
	<script src="${pageContext.request.contextPath}/static/plugin/moment-develop/local-zh-cn.js"></script>
	<script src="${pageContext.request.contextPath}/static/plugin/daterangepicker/daterangepicker.js"></script>
    <script type="text/javascript">
  //给弹框添加回车提交事件
    $(function(){ 
    	$(document).keydown(function(event){ 
	    	if(event.keyCode==13){
	    		submit();
	    	} 
    	}); 
});

  //时间选择框
    $('#daterangepicker').daterangepicker({
        autoApply: true,
    		showDropdowns: true,
    		timePicker: true,
    		timePickerIncrement: 5,
    		timePicker24Hour: true,
        //minDate:moment().subtract(15, 'days'),
        maxDate: new Date(),
//         minDate: moment().subtract(1, 'months'),
//         endDate: new Date(),
        opens: "left",
    		/* dateLimit: {
    		 "days": 15
    		 }, */
        alwaysShowCalendars: true,
        ranges: {
            "今天": [moment().format('YYYY-MM-DD 00:00'),moment().format('YYYY-MM-DD HH:mm')]
            ,"昨天": [moment().subtract(1, 'days').format('YYYY-MM-DD 00:00'),moment().subtract(1, 'days').format('YYYY-MM-DD 23:59')]
            ,"最近7天": [moment().subtract(7, 'days').format('YYYY-MM-DD 00:00'),moment().format('YYYY-MM-DD HH:mm')]
            ,"最近15天": [moment().subtract(15, 'days').format('YYYY-MM-DD 00:00'),moment().format('YYYY-MM-DD HH:mm')]
            ,"本月": [moment().format('YYYY-MM-01 HH:mm'),moment().format('YYYY-MM-DD HH:mm')]
            //,"本季度": [moment().startOf('quarter').format('YYYY-MM-DD'),moment().endOf('quarter').format('YYYY-MM-DD')]
        },
        locale: {
            format: 'YYYY-MM-DD HH:mm',
            separator:" ~ ",
            applyLabel: '确定',
            cancelLabel: '取消',
            customRangeLabel: "自定义日期",
            daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
            monthNames:["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]
        }
    },function (start, end, label) {
        //选择时间之后操作
    	startTime = start.format('YYYY-MM-DD HH:mm');
        endTime = end.format('YYYY-MM-DD HH:mm');
    });
    $(function() {
    	getCondition();
	});
    var currentProjectId = "${currentProjectId}";
    var name = "";
    var state = "";
    var iterationId = "";
    var areaId = "";
    var authorId = "";
    var assignId = "";
    var startTime = "";
    var endTime = "";
    $(function() {
    	//加载表格
		$('#testCaseTable').bootdatagrid(
				"${pageContext.request.contextPath}/testCaseController/listTestCaseByCondition?name="+name
						+"&&state="+state+"&&iterationId="+iterationId+"&&areaId="+areaId+"&&authorId="+authorId
						+"&&assignId="+assignId+"&&startTime="+startTime+"&&endTime="+endTime+"&&projectId="+currentProjectId, {
					pagination : true,
					pageNumber : 1,
					pageSize : 5,
					dataType:'json',
					singleSelect : false,
					striped : true,
					clickToSelect : true,
					onDblClickRow:function(row){
		               addOrEditTestCase(row.testcaseId);
		            }
		});
    	$('#daterangepicker').val("");
	});


    //添加现有用例初始条件信息
    function getCondition(){
    	$.ajax({
		    type : "get",
		    async: false,
		    url : path+'/testCaseController/getCondition?',//找到controller里相应的方法
		    beforeSend: function(XMLHttpRequest){

        	},
		    success : function(dataMap) {
		    	//加载状态下拉列表
		    	var stateList = dataMap.state;
		    	$("#state").html("<option value=''>全部</option>");
		    	$(stateList).each(function(index,state){
		    		$("#state").append("<option value="+state.state+">"+state.name+"</option>");
				});
		    	//加载迭代下拉列表
		    	var iterationList = dataMap.iteration;
		    	$("#iterationId").html("<option value=''>全部</option>");
		    	$(iterationList).each(function(index,iteration){
		    		$("#iterationId").append("<option value="+iteration.id+">"+iteration.name+"</option>");
				});
		    	//加载区域下拉列表
		    	var areaList = dataMap.area;
		    	$("#areaId").html("<option value=''>全部</option>");
		    	$(areaList).each(function(index,area){
		    		$("#areaId").append("<option value="+area.id+">"+area.name+"</option>");
				});
		    	//加载创建人下拉列表
		    	var authorList = dataMap.author;
		    	$("#authorId").html("<option value=''>全部</option>");
		    	$(authorList).each(function(index,author){
		    		$("#authorId").append("<option value="+author.userId+">"+author.userName+"</option>");
				});
		    	//加载指派人下拉列表
		    	var assignerList = dataMap.assigner;
		    	$("#assignId").html("<option value=''>全部</option>");
		    	$(assignerList).each(function(index,assigner){
		    		$("#assignId").append("<option value="+assigner.userId+">"+assigner.userName+"</option>");
				});

		    },
		    complete: function(XMLHttpRequest, textStatus){


        	}
		   });
    };
    //改变状态触发事件
    function stateChange(){
    	state = $('#state option:selected').val();
    };
  //改变迭代触发事件
    function iterationIdChange(){
    	iterationId = $('#iterationId option:selected').val();
    };

  //改变区域触发事件
    function areaIdChange(){
    	areaId = $('#areaId option:selected').val();
    };

  //改变创建人触发事件
    function authorIdChange(){
    	authorId = $('#authorId option:selected').val();
    };
  //改变指派人触发事件
    function assignIdChange(){
    	assignId = $('#assignId option:selected').val();
    };
    function query(){
    	name = $('#name').val();
    	var params={
	            silent: true,
	            url:path+"/testCaseController/listTestCaseByCondition?name="+name
				+"&&state="+state+"&&iterationId="+iterationId+"&&areaId="+areaId+"&&authorId="+authorId
				+"&&assignId="+assignId+"&&startTime="+startTime+"&&endTime="+endTime+"&&projectId="+currentProjectId
	    };
		$('#testCaseTable').bootstrapTable('refresh',params);

    };
    function reset(){
    	$('#name').val("");
    	$('#state').val("");
    	$('#iterationId').val("");
    	$('#areaId').val("");
    	$('#authorId').val("");
    	$('#assignId').val("");
    	$('#daterangepicker').val("");
    	
    	name = "";
        state = "";
        iterationId = "";
        areaId = "";
        authorId = "";
        assignId = "";
        startTime = "";
        endTime = "";
        
    	var params={
	            silent: true,
	            url:path+"/testCaseController/listTestCaseByCondition?name="+name
				+"&&state="+state+"&&iterationId="+iterationId+"&&areaId="+areaId+"&&authorId="+authorId
				+"&&assignId="+assignId+"&&startTime="+startTime+"&&endTime="+endTime+"&&projectId="+currentProjectId
	    };
		$('#testCaseTable').bootstrapTable('refresh',params);

    };
    //保存添加的现有用例
    function submit(){
    	var getSelections = $('#testCaseTable').bootstrapTable('getSelections');
    	var testcaseIds = "";
    	$(getSelections).each(function(index,selection){
    		testcaseIds += testcaseIds==""?"":",";
    		testcaseIds += selection.testcaseId;
		});
		if(testcaseIds == ""){
			toast.info ('请至少选择一条信息！','信息提示'); 
		}else{
			addOrEditTestCase(testcaseIds);
		}
    }

    
    function addOrEditTestCase(testcaseIds){
    	var hierarchyId = parent.nodeId;
    	$.ajax({
	        type : "post",
	        url : path+'/testCaseController/addTestCaseToTestSuite',//找到controller里相应的方法
	        data : {"hierarchyId":hierarchyId,"testcaseIds":testcaseIds}, //把数组的值赋给data，把上面的param改为data,这里就能直接去掉
	        dataType : "json",//根据数据类型选择text、jason等
	        success : function(data) {
	        	if(data.result == true){
	        		parent.addExistingTestCaseFlag = 1;
	        		parent.layer.closeAll();
	        		parent.toast.success('操作成功！','成功提示');
	        	}else{
	        		toast.error('操作失败！','失败提示');
	        	}
	        }
	       });
    }
  //状态列格式转换
	function stateFormatter(value,row,index){
		var stateName = '';
		if(value == 1){
			stateName = "就绪";
		}else if(value == 2){
			stateName = "设计";
		}else if(value == 3){
			stateName = "已关闭";
		}
		return stateName;
	}
  //创建时间Formatter方法
	function timeFormatter(value,row,index){
		var time = new Date(value).Format("yyyy-MM-dd hh:mm");
		return time;
	}
  //转换时间至指定格式显示
	Date.prototype.Format = function (fmt) { // author: meizz
	    var o = {
	        "M+": this.getMonth() + 1, // 月份
	        "d+": this.getDate(), // 日
	        "h+": this.getHours(), // 小时
	        "m+": this.getMinutes(), // 分
	        "s+": this.getSeconds(), // 秒
	        "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
	        "S": this.getMilliseconds() // 毫秒
	    };
	    if (/(y+)/.test(fmt))
	        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	    for (var k in o)
	        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	            return fmt;
	}
    </script>
<jsp:include page="../common/framework/footer.jsp" />